/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoanType.java
 *
 * Created on Dec 29, 2011, 9:32:22 AM
 */
package cnpm;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Quan_Nuce
 */
public class LoanType extends javax.swing.JFrame {
     Connect conn;
     boolean aa;

    /** Creates new form LoanType */
    public LoanType() throws SQLException {
        initComponents();
          Load();
       
    }
       private void Search() throws SQLException
    {
      //Load1("Select * From LoanType where LoanTypeID like '[ "+jTextField1.getText()+"]% '");
        Load1("Select * From LoanType where LoanTypeID=N'"+jTextField1.getText()+"'");
    }
       private void Load() throws SQLException
{
    Load1("Select * From LoanType");
}
       private void Load1(String aaa) throws SQLException
    {
         conn = new Connect();
        ResultSet rs=conn.excuteQuery(aaa);
        
        DefaultTableModel dtm = new DefaultTableModel();
        dtm.setNumRows(0);
        
        Vector Columnname = new Vector();
        Columnname.addElement("LoanTypeID");
        Columnname.addElement("Description");
      
             
        
        dtm.setColumnIdentifiers(Columnname);
        
        while(rs.next())
        {
            Vector row = new Vector();
            row.addElement(rs.getObject(1).toString());
            row.addElement(rs.getObject(2).toString());
            
            
            
            dtm.addRow(row);
        }
        jTable1.setModel(dtm);
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        txtLoanType = new javax.swing.JTextField();
        txtDes = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        them = new javax.swing.JButton();
        sua = new javax.swing.JButton();
        luu = new javax.swing.JButton();
        xoa = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        timkiem = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Title 1", "Title 2"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jLabel2.setText("LoanTypeID");

        jLabel3.setText("Description");

        them.setText("Thêm");
        them.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                themMouseClicked(evt);
            }
        });

        sua.setText("Sửa");
        sua.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                suaMouseClicked(evt);
            }
        });

        luu.setText("Lưu");
        luu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                luuMouseClicked(evt);
            }
        });

        xoa.setText("Xóa");
        xoa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                xoaMouseClicked(evt);
            }
        });

        jButton5.setText("Cancel");
        jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton5MouseClicked(evt);
            }
        });

        timkiem.setText("Tìm kiếm");
        timkiem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                timkiemMouseClicked(evt);
            }
        });
        timkiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                timkiemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(85, 85, 85)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(160, 160, 160)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(56, 56, 56)
                        .addComponent(timkiem))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(them)
                            .addComponent(jLabel2))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(51, 51, 51)
                                .addComponent(txtLoanType, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(29, 29, 29)
                                .addComponent(jLabel3)
                                .addGap(28, 28, 28)
                                .addComponent(txtDes, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(64, 64, 64)
                                .addComponent(sua)
                                .addGap(61, 61, 61)
                                .addComponent(luu)
                                .addGap(62, 62, 62)
                                .addComponent(xoa)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                                .addComponent(jButton5)))))
                .addContainerGap(40, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(82, 82, 82)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(timkiem))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtLoanType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(txtDes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(them)
                    .addComponent(luu)
                    .addComponent(xoa)
                    .addComponent(sua)
                    .addComponent(jButton5))
                .addGap(170, 170, 170))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void timkiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_timkiemActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_timkiemActionPerformed

private void timkiemMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_timkiemMouseClicked
// TODO add your handling code here:
     try {
            // TODO add your handling code here:
                Search();
        } catch (SQLException ex) {
            Logger.getLogger(LoanType.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_timkiemMouseClicked

private void themMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_themMouseClicked
// TODO add your handling code here:
    aa  = true;
    txtLoanType.setText("");
    txtDes.setText("");
   
    
}//GEN-LAST:event_themMouseClicked

private void suaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_suaMouseClicked
// TODO add your handling code here:
    aa=false;
    txtLoanType.setEnabled(false);
}//GEN-LAST:event_suaMouseClicked

private void luuMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_luuMouseClicked
// TODO add your handling code here:
    conn = new Connect();
    if (aa==true)
    {
        try {
            int a = conn.executeUpdate("insert into LoanType values('"+txtLoanType.getText()+"','"+txtDes.getText()+"')");
        } catch (SQLException ex) {
            Logger.getLogger(LoanType.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            Load();
        } catch (SQLException ex) {
            Logger.getLogger(LoanType.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    if (aa==false)
    {
            try {
                int b = conn.executeUpdate("update LoanType set Description='"+txtDes.getText()+"' where LoanTypeID='"+txtLoanType.getText()+"'");
                Load();
            } catch (SQLException ex) {
                Logger.getLogger(LoanType.class.getName()).log(Level.SEVERE, null, ex);
            }
            
    }
}//GEN-LAST:event_luuMouseClicked

private void xoaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_xoaMouseClicked
// TODO add your handling code here:
    conn = new Connect();
       int i = jTable1.getSelectedRow();
       String b = (String) jTable1.getValueAt(i, 0);
        int j= (JOptionPane.showConfirmDialog(null, "Bạn Thực Sự Muốn Xóa !","Thông Báo",JOptionPane.YES_NO_OPTION));
        if (j==JOptionPane.YES_OPTION)
        {
       
        try {
            int a = conn.executeUpdate("Delete from LoanType where LoanTypeID='"+b+"'");
        } catch (SQLException ex) {
            Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            Load();
        } catch (SQLException ex) {
            Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
}//GEN-LAST:event_xoaMouseClicked

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
// TODO add your handling code here:
    int i = jTable1.getSelectedRow();
   txtLoanType.setText(jTable1.getValueAt(i, 0).toString());
   txtDes.setText(jTable1.getValueAt(i, 1).toString());
}//GEN-LAST:event_jTable1MouseClicked

private void jButton5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton5MouseClicked
// TODO add your handling code here:
    this.dispose();
}//GEN-LAST:event_jButton5MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LoanType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LoanType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LoanType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LoanType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new LoanType().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(LoanType.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JButton luu;
    private javax.swing.JButton sua;
    private javax.swing.JButton them;
    private javax.swing.JButton timkiem;
    private javax.swing.JTextField txtDes;
    private javax.swing.JTextField txtLoanType;
    private javax.swing.JButton xoa;
    // End of variables declaration//GEN-END:variables
}
